//
//  LLGuidanceViewController.m
//  PandaMakeUp
//
//  Created by dl on 16/1/4.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#define TITLE_LABELS                @[@"缤纷大牌 一站促达", @"品牌供货 品质保障", @"热门专题 精心挑选", @"会员福利 心跳价格"]

#import "LLGuidanceViewController.h"

@interface LLGuidanceViewController () <UIScrollViewDelegate>
@property(strong, nonatomic) IBOutletCollection(UIView) NSArray *dots;
@property(weak, nonatomic) IBOutlet UILabel *textLabel;
@property(weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property(strong, nonatomic) IBOutletCollection(UILabel) NSArray *titleLabels;
@property(weak, nonatomic) IBOutlet UIButton *entryBtn;
@property(weak, nonatomic) IBOutlet UIButton *cancleBtn;

@property(nonatomic, assign) int curPage;
@end

@implementation LLGuidanceViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.view.frame = [UIScreen mainScreen].bounds;
        _curPage = 0;
        if (DEVICE_IS_IPHONE_4_OR_5 && FULL_HEIGHT < 568.f) {
            self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"guidance_bg_4"]];
        } else if (DEVICE_IS_IPHONE_4_OR_5) {
            self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"guidance_bg_5"]];
        } else if (DEVICE_IS_IPHONE_6) {
            self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"guidance_bg_6"]];
        } else if (DEVICE_IS_IPHONE_6_PLUS) {
            self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"guidance_bg_6P"]];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    for (UIView *dot in _dots) {
        dot.layer.borderWidth = 1.f;
        dot.layer.borderColor = UIColorFromRGB(0xFF92CC).CGColor;
        dot.backgroundColor = UIColorFromRGBA(0xFF92CC, 0);
    }
    // 原地滚动下
    [self scrollViewDidScroll:_scrollView];
}

- (IBAction)close {
    [UIView animateWithDuration:0.5 animations:^{
        self.view.alpha = 0;
    }                completion:^(BOOL finished) {
        [self removeFromParentViewController];
        [self.view removeFromSuperview];
    }];
}

#pragma mark -
#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageFloat = scrollView.contentOffset.x / scrollView.ll_width;
    if (pageFloat > 3) {
        return;
    }
    [_dots setValue:@(1.f) forKeyPath:@"alpha"];
    int nextPage = pageFloat > _curPage ? (_curPage + 1) : (_curPage - 1);
    nextPage = nextPage < 0 ? 0 : (nextPage > 3 ? 3 : nextPage);
    UIView *curDot = _dots[_curPage];
    UIView *nextDot = _dots[nextPage];

    CGFloat radius = ABS(pageFloat - _curPage);

    // title文本
    UILabel *curLabel = (UILabel *) _titleLabels[_curPage % 2];
    UILabel *nextLabel = (UILabel *) _titleLabels[nextPage % 2];
    curLabel.text = TITLE_LABELS[_curPage];
    nextLabel.text = TITLE_LABELS[nextPage];

    curLabel.alpha = 1 - radius;
    curDot.backgroundColor = UIColorFromRGBA(0xFF92CC, 1 - radius);
    if (curDot != nextDot) {
        nextDot.backgroundColor = UIColorFromRGBA(0xFF92CC, radius);
        nextLabel.alpha = radius;
    }
    // 跳过 按钮
    if (pageFloat > 2 && pageFloat > _curPage) {
        _cancleBtn.alpha = 1 - radius;
    } else if (pageFloat > 2) {
        _cancleBtn.alpha = radius;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    _scrollView.scrollEnabled = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageFloat = scrollView.contentOffset.x / scrollView.ll_width;
    if (ABS(pageFloat - 3) < 0.05) {
        [UIView animateWithDuration:0.5f animations:^{
            _entryBtn.alpha = 1.f;
            [_dots setValue:@(0.f) forKeyPath:@"alpha"];

        }];
    } else {
        _entryBtn.alpha = 0.f;
    }
    _curPage = roundf(pageFloat);
    _scrollView.scrollEnabled = YES;
}

@end
